from PySide6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from qfluentwidgets import BodyLabel, LineEdit, PushButton, FluentIcon

from app.utils.BaseMsg import MsgLatOrLon
from app.utils.wrappers import check_lat_lon2


class FloatToLatLon2(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.setObjectName("经纬度转换浮点数")

        self.label_lon = BodyLabel()
        self.label_lon.setText("经度：")
        # self.label_lon.setTextColor(QColor(0, 255, 0), QColor(255, 0, 0))

        self.input_lon = LineEdit()
        self.input_lon.setPlaceholderText("120.5555")
        self.input_lon.setClearButtonEnabled(True)

        self.label_lat = BodyLabel()
        self.label_lat.setText("纬度：")
        # self.label_lat.setTextColor(QColor(0, 255, 0), QColor(255, 0, 0))

        self.input_lat = LineEdit()
        self.input_lat.setPlaceholderText("55.5555")
        self.input_lat.setClearButtonEnabled(True)

        self.button_conv = PushButton(FluentIcon.SYNC, "转换")
        self.button_clear = PushButton(FluentIcon.CLEAR_SELECTION, "清空")

        lon_layout = QHBoxLayout()
        lon_layout.addWidget(self.label_lon)
        lon_layout.addWidget(self.input_lon)

        lat_layout = QHBoxLayout()
        lat_layout.addWidget(self.label_lat)
        lat_layout.addWidget(self.input_lat)

        button_layout = QHBoxLayout()
        button_layout.addWidget(self.button_conv)
        button_layout.addWidget(self.button_clear)

        layout = QVBoxLayout()
        layout.addLayout(lon_layout)
        layout.addLayout(lat_layout)
        layout.addLayout(button_layout)

        self.setLayout(layout)

        self.button_conv.clicked.connect(self.convert)
        self.button_clear.clicked.connect(self.clear)

    @check_lat_lon2
    def check_lat_lon2(self, lat, lon):
        return lat, lon

    def convert(self):
        __lat = self.input_lat.text()
        __lon = self.input_lon.text()
        lat, lon = self.check_lat_lon2(__lat, __lon)
        tt = MsgLatOrLon(
            lat,
            lon,
            self.input_lon.text(),
            self.input_lat.text(),
            self)
        if tt.exec():
            print(tt.old_label_lon.text(), tt.old_label_lat.text())

    def clear(self):
        self.input_lat.clear()
        self.input_lon.clear()
